package cn.tedu.fy.pethome.controller;

import cn.tedu.fy.pethome.pojo.dto.CartAddDTO;
import cn.tedu.fy.pethome.pojo.dto.CartUpdateDTO;
import cn.tedu.fy.pethome.pojo.restful.JsonPage;
import cn.tedu.fy.pethome.pojo.vo.CartStandardVO;
import cn.tedu.fy.pethome.security.LoginPrincipal;
import cn.tedu.fy.pethome.service.ICartService;
import cn.tedu.fy.pethome.util.WebConsts;
import cn.tedu.fy.pethome.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车管理模块")
@Slf4j
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping("/add")
    @ApiOperation("新增购物车信息")
    public JsonResult addCart(@AuthenticationPrincipal LoginPrincipal loginPrincipal,@Validated CartAddDTO cartAddDTO){
        log.debug("添加购物车控制器执行了,传入了参数CartAddDTO:{}",cartAddDTO);
        cartService.addCart(cartAddDTO,loginPrincipal);
        log.debug("添加购物车控制器执行完毕了");
        return JsonResult.ok("新增购物车完成");
    }

    // 分页查询用户购物车中商品信息
    @GetMapping("/list")
    @ApiOperation("查询用户购物车中商品信息")
    @PreAuthorize("hasRole('user')")
    public JsonResult<List<CartStandardVO>> listCartByPage(@AuthenticationPrincipal LoginPrincipal loginPrincipal){
        // 正常调用业务逻辑层并返回
        List<CartStandardVO> cartStandardVOs = cartService.listCarts(loginPrincipal);
        log.debug("返回的购物车列表:{}",cartStandardVOs);
        return JsonResult.ok(cartStandardVOs);

    }

    // 根据id的数组删除购物车中sku商品的方法
    //    @ApiImplicitParam(value = "要删除的购物车id数组",name = "id",
//            required = true,dataType = "Long")
    @GetMapping("/delete/{id}")
    @ApiOperation("根据id的数组删除购物车中sku商品")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult removeCartsByIds(@PathVariable Long id){
        log.debug("开始删除");
        cartService.removeCart(id);
        return JsonResult.ok();
    }

    // 清空当前登录用户的购物车
    @PostMapping("/delete/all")
    @ApiOperation("清空当前登录用户的购物车")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeCartsByUserId(@AuthenticationPrincipal LoginPrincipal loginPrincipal){
        cartService.removeAllCarts(loginPrincipal);
        return JsonResult.ok("购物车清空完成");
    }

    // 修改购物车数量
    @PostMapping("/update/quantity")
    @ApiOperation("修改购物车数量")
    @PreAuthorize("hasRole('user')")
    public JsonResult updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        cartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("修改完成!");
    }
}
